// License Notification
//======================================================
// Copyright (c) 2010 Channel Intelligence Inc.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// ======================================================




using System;
using System.Linq;
using System.Reflection;

namespace CIP4.Item
{
    [AttributeUsage(AttributeTargets.Property)]
    public class CIP4PropertyAttribute : Attribute
    {
        public static CIP4PropertyAttribute GetAttribute(PropertyInfo forProp)
        {
            object[] att = forProp.GetCustomAttributes(typeof(CIP4PropertyAttribute), false);
            return att.Count() > 0 ? (CIP4PropertyAttribute)att[0] : null;
        }

        public static CIP4PropertyAttribute GetAttribute(PropertyInfo property, CIP4ItemAttribute itemAttribute)
        {
            object[] attributes = property.GetCustomAttributes(typeof(CIP4PropertyAttribute), false);
            if (attributes.Count() > 0)
                return (CIP4PropertyAttribute)attributes[0];
            
            return new CIP4PropertyAttribute { DontSerialize = !itemAttribute.AddAllProperties };
        }

        /// <summary>
        /// If specified, determines the order in which the property appears in the serialization. Note that CLR
        /// reflection does not provide a mechanism for determining the declared order. If Ordinal is not specified
        /// the properties will be serialized in alphabetical order. Any properties with the same Ordinal will
        /// be ordered firstly by Ordinal and then by name. The value zero will be ignored.
        /// </summary>
        public int Ordinal { get; set; }

        /// <summary>
        /// If true, the associated property will not be serialized and will not be included in any generated CIP4 type
        /// </summary>
        public bool DontSerialize { get; set; }

        /// <summary>
        /// If true, the target type for the property will be serialized even if it does not have a CIP4ItemAttribute
        /// </summary>
        public bool SerializeTarget { get; set; }

        /// <summary>
        /// If true - indicates to any underlying DataProvider that the property sould be indexed if possible
        /// </summary>
        public bool IsIndexed { get; set; }
    }
}
